
/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  DevideLogwithTime.java                                      
 *  File Contents Outline :  切割日志                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.util;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author 2600
 */
public class DevideLogwithTime {

    private static String fromPath;
    private static ArrayList<String> products;
    private static String startTime;
    private static String endTime;
    private static String toPath;
    private static ArrayList<String> all;
    private static int originalSize;

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        all = new ArrayList<String>();
        originalSize = 0;
        products = new ArrayList<String>();

        startTime = "201205000000";
        endTime = "201305100000";
       // products.add("other");
        fromPath = "E:/盘古日志0419/2012.6/";
        //String fromPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021508/pagesearch_2012021508";
        //String fromPath = "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215.log";
        //String fromPath = "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0.log";



        File file = new File(fromPath);
        readFile(file);


        Collections.sort(all);

        toPath = "E:/盘古日志0419/vstar_sorted.log";
        //String toPath = "E:/pangu_new/3.pagesearch-hdfs-20120215-16/pagesearch-hdfs-20120215-16/duration=2012021508/pagesearch_2012021508_new1.log";
        //String toPath = "E:/pangu_new/2.pagesearch-file-20120215-16/pagesearch_20120215_New.log";
        //String toPath = "E:/pangu_new/1.pagesearch-ip-20120215-16/collect_temp/pagesearch/10.10.128.13/pagesearch_201202150000_0_new.log";

        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
                toPath)));
        int resultSize = 0;
        for (int i = 0; i < all.size(); i++) {
            if (all.get(i) != null) {
                writer.write(all.get(i) + "\n");
                resultSize++;
            }
        }
        writer.close();

   //     System.out.println("originalSize:" + originalSize);
//        System.out.println("resultSize:" + resultSize);
    }

    /**
     * reader for file/Directory
     *
     * @param readfile
     */
    private static void readFile(File readfile) throws FileNotFoundException, IOException {
        if (!readfile.isDirectory()) {
            readFileContent(readfile);
        } else if (readfile.isDirectory()) {
            File[] fileList = readfile.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                File editfile = fileList[i];
               

                if (!editfile.isDirectory()) {
                    readFileContent(editfile);
                } else if (editfile.isDirectory()) {
                    readFile(editfile);
                }
            }
        }
    }

    private static void readFileContent(File readfile) throws FileNotFoundException, IOException {
        String fileName = readfile.getName();
        String[] fileDetail = fileName.split("_");
        String product = fileDetail[0];
        String time = fileDetail[1];
        if (time.compareTo(startTime) >= 0 && time.compareTo(endTime) < 0) {//in the timezone
            if (products.size() == 0) {//no product restrain
                readMatchFile(readfile);
            } else {
                for (int i = 0; i < products.size(); i++) {
                    if (product.contains(products.get(i))) {
                        readMatchFile(readfile);
                    }

                }
            }
        }



    }

    private static void readMatchFile(File matchFile) throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new FileReader(matchFile));
        String record = reader.readLine();
        while (record != null) {
            all.add(record);
            originalSize++;
            record = reader.readLine();
        }
        reader.close();
    }
}
